package dbutils.dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class Demo2 {
    ComboPooledDataSource dataSource =new ComboPooledDataSource();
    QueryRunner qr = new QueryRunner();
    @Test
    public void account(){
        Connection con =null;
        try {
            con = dataSource.getConnection();
            con.setAutoCommit(false);
        String sql ="update account set money= money-? where name=? ";
            String s = qr.update(con, sql, 300, "pgone") > 0 ? "转钱成功可以嫖娼" : "转钱失败,没钱还想白嫖";
            System.out.println(s);
            String sql1 = "update account set money= money+? where name=?";
            String s1 = qr.update(con, sql1, 300, "lxl") > 0 ? "大爷来玩啊" : "没钱还想白嫖";
            System.out.println(s1);
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
